// src/renderer/utils/logger.ts
import { LogMessage } from '../../../LogService/LogType';

export const logToWorker = (
  level: LogMessage['level'],
  logger: string,
  extra?: T // 接收泛型数据
) => {
  if (!window.electronAPI?.logToWorker) {
    console.warn('Electron API not available, fallback to console.log');
    console.log(`[${level}] ${logger}`, extra);
    return;
  }

  const logMessage: LogMessage = {
    level,
    logger,
    timestamp: new Date().toISOString(),
    ip: '192.168.10.47', // 实际项目中应从主进程获取真实IP
    pid: 0,     // 渲染进程无真实PID，用0占位
    extra,
  };

  window.electronAPI.logToWorker(logMessage);
};

// 快捷方法
export const logger = {
  // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
  verbose: <T>(message: string, extra?: T) => 
    logToWorker('VERBOSE', message, extra),
  
  // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
  info: <T>(message: string, extra?: T) => 
    logToWorker('INFO', message, extra),
  
  // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
  warn: <T>(message: string, extra?: T) => 
    logToWorker('WARN', message, extra),
  
  // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
  error: <T>(message: string,  extra?: T) =>
    logToWorker('ERROR', message, extra),
};